﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BF.IBLL.Dictionary;
using Base.Core.UnityFactory;
using BF.Model;
using System.Web.Script.Serialization;

namespace BF.Web.pages.BaseSystem.UserManagement
{
    public class NodeInfo
    {
        public string gKey;
        public string text;
        public bool leaf;
        public string icon = "/images/icons/house.png";
        public bool expanded = false;
        public List<NodeInfo> children = new List<NodeInfo>();
    }

    /// <summary>
    /// HHospitalTree 的摘要说明
    /// </summary>
    public class HHospitalTree : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string gKey = context.Request.QueryString["gKey"] ?? default(Guid).ToString();
            string gFatherKey = context.Request.QueryString["gFatherKey"] ?? default(Guid).ToString();
            Idic_OrganizationBLL iBLL = IOCFactory.GetIOCResolve<Idic_OrganizationBLL>();
            List<dic_Organization> allList = iBLL.GetList(p => p.iUse == 1).ToList();
            if (gFatherKey == default(Guid).ToString())
            {
                dic_Organization benjigou = allList.FirstOrDefault(p => p.gKey == new Guid(gKey));
                NodeInfo rootNode = new NodeInfo();
                rootNode.gKey = benjigou.gKey.ToString();
                rootNode.text = benjigou.sOrganizationName;
                rootNode.expanded = true;
                LoadNodes(rootNode, allList);
                List<NodeInfo> jsdata = new List<NodeInfo>();
                jsdata.Add(rootNode);
                string strJson = ToJson(jsdata.ToArray()).Replace("Checked", "checked");
                strJson = strJson.Replace("\"\\u003c\\u003c", "{").Replace("\\u003e\\u003e\"", "}").Replace("\\u0027", "").Replace("\\\"", "'");
                context.Response.Write(strJson);
            }
            else
            {
                List<dic_Organization> list = allList.Where(p => p.gHigherKey == new Guid(gFatherKey))
                    .OrderBy(p => p.sOrganizationType).ThenBy(p => p.sOrganizationCode).ToList();
                List<NodeInfo> jsdata = new List<NodeInfo>();
                foreach (dic_Organization item in list)
                {
                    NodeInfo node = new NodeInfo();
                    node.gKey = item.gKey.ToString();
                    node.text = item.sOrganizationName;
                    LoadNodes(node, allList);
                    jsdata.Add(node);
                }
                string strJson = ToJson(jsdata.ToArray()).Replace("Checked", "checked");
                strJson = strJson.Replace("\"\\u003c\\u003c", "{").Replace("\\u003e\\u003e\"", "}").Replace("\\u0027", "").Replace("\\\"", "'");
                context.Response.Write(strJson);
            }
        }

        private void LoadNodes(NodeInfo parentNode, List<dic_Organization> allList)
        {
            List<dic_Organization> currentList = allList
                   .Where(P => P.gHigherKey.Equals(new Guid(parentNode.gKey)))
                   .OrderBy(P => P.sAreaCode)
                   .ToList();
            parentNode.leaf = currentList.Count == 0;
            foreach (dic_Organization item in currentList)
            {
                NodeInfo node = new NodeInfo();
                node.gKey = item.gKey.ToString();
                node.text = item.sOrganizationName;
                parentNode.children.Add(node);
                LoadNodes(node, allList);
            }
        }

        public string ToJson(object o)
        {
            //序列化对象为json数据，很重要！
            JavaScriptSerializer j = new JavaScriptSerializer();
            return j.Serialize(o);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}